﻿using MaterialDesignThemes.Wpf;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using UpperComputer.WPF.Entry.Common;
using UpperComputer.WPF.Entry.Common.Extensions;

namespace UpperComputer.WPF.Entry.Controls
{
    /// <summary>
    /// DialogTop.xaml 的交互逻辑
    /// </summary>
    public partial class DialogTop : UserControl
    {
        /// <summary>
        /// 对话框关闭时路由事件声明和注册
        /// </summary>
        private static readonly RoutedEvent OnDialogClosingEvent = EventManager.RegisterRoutedEvent(nameof(OnDialogClosing), RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(DialogTop));

        private static readonly DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(DialogTop), new PropertyMetadata(string.Empty, null, TitleCoerceValueCallback));

        private static readonly DependencyProperty DialogIdentifierProperty = DependencyProperty.Register(nameof(DialogIdentifier), typeof(string), typeof(DialogTop), new PropertyMetadata(Constant.DIALOG_IDENTIFIER_ROOT));

        private static readonly DependencyProperty CancelCommandProperty = DependencyProperty.Register(nameof(CancelCommand), typeof(ICommand), typeof(DialogTop));

        private static readonly DependencyProperty CancelCommandParameterProperty = DependencyProperty.Register(nameof(CancelCommandParameter), typeof(object), typeof(DialogTop));

        /// <summary>
        /// 对话框关闭时事件
        /// </summary>
        [Category("自定义事件"), Description("对话框关闭时事件")]
        public event RoutedEventHandler OnDialogClosing
        {
            add { AddHandler(OnDialogClosingEvent, value); }
            remove { RemoveHandler(OnDialogClosingEvent, value); }
        }

        [Bindable(true), Category("自定义属性"), Description("窗体顶部标题")]
        public string Title
        {
            get => (string)GetValue(TitleProperty);
            set => SetValue(TitleProperty, value);
        }

        public string DialogIdentifier
        {
            get => (string)GetValue(DialogIdentifierProperty);
            set => SetValue(DialogIdentifierProperty, value);
        }

        [Bindable(true), Category("自定义属性"), Description("取消命令")]
        public ICommand CancelCommand
        {
            get => (ICommand)GetValue(CancelCommandProperty);
            set => SetValue(CancelCommandProperty, value);
        }

        [Bindable(true), Category("自定义属性"), Description("取消命令参数")]
        public object? CancelCommandParameter
        {
            get => GetValue(CancelCommandParameterProperty);
            set => SetValue(CancelCommandParameterProperty, value);
        }

        public DialogTop()
        {
            InitializeComponent();
        }

        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            if (CancelCommand != null && CancelCommand.CanExecute(CancelCommandParameter))
            {
                CancelCommand.Execute(CancelCommandParameter);
            }
            CloseDialog();
        }

        public void CloseDialog(object parameter = null)
        {
            RoutedEventArgs eventArgs = new RoutedEventArgs(OnDialogClosingEvent, this);
            RaiseEvent(eventArgs);
            DialogHost.Close(DialogIdentifier, parameter);
        }

        private static object TitleCoerceValueCallback(DependencyObject d, object baseValue)
        {
            if (baseValue == null)
            {
                return baseValue;
            }
            string title = baseValue.ToString();
            ((DialogTop)d).TitleLabel.Content = title;
            return title;
        }
    }
}
